package com.haojiang.mh.ui.activity

import com.haojiang.mh.R
import com.haojiang.mh.cache.Const
import com.haojiang.mh.service.ble.BleHandle
import com.haojiang.mh.service.event.OneEvent
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.ActManager
import com.haojiang.mh.utils.SpUtil
import kotlinx.android.synthetic.main.activity_choose_user_mode.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 选择用户单双人模式
 *
 * Created by ZapFive on 2019-06-01.
 *
 * wuzhuang@mirahome.me
 */
class ChooseUserModeActivity : BaseActivity() {

    override fun getLayoutId() = R.layout.activity_choose_user_mode

    override fun getTitleName() = ""

    override fun initActivity() {
    }

    override fun initView() {
        tv_single_mode.setOnClickListener { BleHandle.instance.sendModelOperate(false) }
        tv_double_mode.setOnClickListener { BleHandle.instance.sendModelOperate(true) }
    }

    override fun onBackClick() {
        // do nothing
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceicerEvent(event: OneEvent) {
        when (event.type) {
            Const.WRITE_CALLBACK -> {
                if (event.getIntValue() != Const.OK_RESULT) {
                    showToast(R.string.txt_write_data_fail)
                }
            }

            Const.USER_MODE -> {
                SpUtil.put(Const.IS_DOUBLE_MODE, event.getIntValue() == 2)
                EventBus.getDefault().post(OneEvent(Const.JUMP_MAIN))
                ActManager.redirectToBottomActivity()
                context.finish()
            }
        }
    }
}